package com.neusoft.neuiotms.dcam.fixed.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.ConnectionRelationDTO;
import com.neusoft.neuiotms.dcam.fixed.model.ConnectionRelation;
import com.neusoft.neuiotms.dcam.fixed.repository.ConnectionRelationRepository;

@Component
public class ConnectionRelationConverter extends AbstractConverter<ConnectionRelation, ConnectionRelationDTO> {

    @Autowired
    private ConnectionRelationRepository relationRepository;

    @Override
    public ConnectionRelation toModel(final ConnectionRelationDTO dto) {
        ConnectionRelation model;
        if (dto.isNew()) {
            model = new ConnectionRelation();
        } else {
            model = this.relationRepository.getOne(dto.getId());
        }

        model.setBusinessId(dto.getBusinessId());
        model.setName(dto.getName());

        return model;
    }

    @Override
    public ConnectionRelationDTO toDTO(final ConnectionRelation model, final boolean forListView) {
        final ConnectionRelationDTO dto = new ConnectionRelationDTO();
        dto.setId(model.getId());
        dto.setBusinessId(model.getBusinessId());
        dto.setName(model.getName());
        return dto;
    }

}
